﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Xml;
using System.Xml.XPath;

namespace HsCmpScm
{
    public class ScmConfiguration
    {
        static XmlDocument ConfigXmlDoc;
        static String PageTitle;
        static String CompanyName;
        static String DepName;

        public String ErrorMsg;

        public Boolean CheckConfigXmlFile()
        {
            ConfigXmlDoc = new XmlDocument();

            try
            {
                //注意：不同页面对于xml文件的相对位置不同。
                //例如Default.aspx和Login.aspx。
                //此处应为相对路径。
                ConfigXmlDoc.Load(HttpContext.Current.Server.MapPath("./Configure/DefaultPageCfg.xml"));
                return true;
            }
            catch (HttpException e)
            {
                this.ErrorMsg = "HttpException:" + e.Message;
                return false;
            }
            catch (DirectoryNotFoundException e)
            {
                this.ErrorMsg = "DirectoryNotFoundException:" + e.Message;
                return false;
            }
            catch (FileNotFoundException e)
            {
                this.ErrorMsg = "FileNotFoundException:" + e.Message;
                return false;
            }
            catch (XmlException e)
            {
                this.ErrorMsg = "XmlException:" + e.Message;
                return false;
            }                    
        }

        public Boolean ConfigIsCompleted()
        {
            XmlNode IsCompleteNode;
            ConfigXmlDoc.Load(HttpContext.Current.Server.MapPath("Configure/DefaultPageCfg.xml"));
            try
            {
                IsCompleteNode = ConfigXmlDoc.SelectSingleNode("configuration/isCompleted");
            }
            catch (XPathException e)
            {
                this.ErrorMsg = "XPathException:" + e.Message;
                return false;
            }
            catch (NullReferenceException e)
            {
                this.ErrorMsg = "NullReferenceException:" + e.Message;
                return false;
            }
            try
            {
                if(String.IsNullOrEmpty(IsCompleteNode.InnerText))
                {
                    this.ErrorMsg = "[configuration/isCompleted] InnerText is NULL.";
                    return false;
                }
                else if (IsCompleteNode.InnerText.Equals("true"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (NullReferenceException e)
            {
                this.ErrorMsg = "NullReferenceException:" + e.Message;
                return false;
            }
        }

        public Boolean CheckDataBaseConfig()
        {
            XmlNode DbConfNode;
            XmlNode DbAddrNode;
            XmlNode DbNameNode;
            XmlNode DbUidNode;
            XmlNode DbPwdNode;

            ConfigXmlDoc.Load(HttpContext.Current.Server.MapPath("Configure/DefaultPageCfg.xml"));
            try
            {
                DbConfNode = ConfigXmlDoc.SelectSingleNode("configuration/databaseConfig/SqlConfig");
                DbAddrNode = ConfigXmlDoc.SelectSingleNode("configuration/databaseConfig/SqlConfig/db_Address");
                DbNameNode = ConfigXmlDoc.SelectSingleNode("configuration/databaseConfig/SqlConfig/db_Name");
                DbUidNode = ConfigXmlDoc.SelectSingleNode("configuration/databaseConfig/SqlConfig/db_Uid");
                DbPwdNode = ConfigXmlDoc.SelectSingleNode("configuration/databaseConfig/SqlConfig/db_Pwd");               
            }
            catch (XPathException e)
            {
                this.ErrorMsg = "XPathException:" + e.Message;
                return false;
            }
            catch (NullReferenceException e)
            {
                this.ErrorMsg = "NullReferenceException:" + e.Message;
                return false;
            }

            try
            {
                if (String.IsNullOrEmpty(DbAddrNode.InnerText))
                {
                    this.ErrorMsg = "[configuration/databaseConfig/SqlConfig/db_Address] InnerText is NULL.";
                    return false;
                }
                else if (String.IsNullOrEmpty(DbNameNode.InnerText))
                {
                    this.ErrorMsg = "<br />[configuration/databaseConfig/SqlConfig/db_Name] InnerText is NULL.";
                    return false;
                }
                else if (String.IsNullOrEmpty(DbUidNode.InnerText))
                {
                    this.ErrorMsg = "[configuration/databaseConfig/SqlConfig/db_Uid] InnerText is NULL.";
                    return false;
                }
                else if (String.IsNullOrEmpty(DbPwdNode.InnerText))
                {
                    this.ErrorMsg = "[configuration/databaseConfig/SqlConfig/db_Pwd] InnerText is NULL.";
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (NullReferenceException e)
            {
                this.ErrorMsg = "NullReferenceException:" + e.Message;
                return false;
            }            
        }

        public String GetPageTitle()
        {
            XmlNode PageTitleNode;
            ConfigXmlDoc.Load(HttpContext.Current.Server.MapPath("Configure/DefaultPageCfg.xml"));
            try
            {
                //参考资料
                //http://msdn.microsoft.com/en-us/library/h0hw012b(v=vs.110).aspx
                //http://msdn.microsoft.com/zh-cn/library/system.xml.xmldocument.selectsinglenode(v=vs.110).aspx
                //http://msdn.microsoft.com/zh-cn/library/d271ytdx(v=vs.110).aspx
                //http://blog.csdn.net/wf520pb/article/details/2644549

                PageTitleNode = ConfigXmlDoc.SelectSingleNode("configuration/pageConfig/title_String");
            }
            catch (XPathException e)
            {
                return "PageTitleNode XPathException:" + e.Message;
            }
            try
            {
                PageTitle = PageTitleNode.InnerText;
            }
            catch (NullReferenceException e)
            {
                return "PageTitleNode NullReferenceException:" + e.Message;
            }

            if (String.IsNullOrEmpty(PageTitle))
            {
                PageTitle = "供应商系统";
            }

            return PageTitle;
        }

        public String GetCompanyName()
        {
            XmlNode CompanyNameNode;
            ConfigXmlDoc.Load(HttpContext.Current.Server.MapPath("Configure/DefaultPageCfg.xml"));
            try
            {
                CompanyNameNode = ConfigXmlDoc.SelectSingleNode("configuration/pageConfig/company_Name");
            }
            catch (XPathException e)
            {
                return "CompanyNameNode XPathException:" + e.Message;
            }
            try
            {
                CompanyName = CompanyNameNode.InnerText;
            }
            catch (NullReferenceException e)
            {
                return "CompanyNameNode NullReferenceException:" + e.Message;
            }

            if (String.IsNullOrEmpty(CompanyName))
            {
                CompanyName = "";
            }

            return CompanyName;
        }

        public String GetDepName()
        {
            XmlNode DepNameNode;
            ConfigXmlDoc.Load(HttpContext.Current.Server.MapPath("Configure/DefaultPageCfg.xml"));
            try
            {
                DepNameNode = ConfigXmlDoc.SelectSingleNode("configuration/pageConfig/dep_Name");
            }
            catch (XPathException e)
            {
                return "DepNameNode XPathException:" + e.Message;
            }
            try
            {
                DepName = DepNameNode.InnerText;
            }
            catch (NullReferenceException e)
            {
                return "DepNameNode NullReferenceException:" + e.Message;
            }

            return DepName;
        }
    }
}